<?php

class Character extends Validation {
	private $name = null;
	private $level = 1;
	private $points = 0;
	private $race = 0;
	private $experience = 0;
	private $strength = 0;
	private $agility = 0;
	private $vitality = 0;
	private $energy = 0;
	private $money = 0;
	private $map = 0;
	private $position_x = 0;
	private $position_y = 0;
	private $code = 0;
	
	private $data = array();
	private $validates = array();
	
	private $config = null;
	
	public function __construct($name = null) {
		$this->config = new Config();
		if (!is_null($name)) {
			$this->name($name);
			$this->find($name);
		}
	}

	public function errors() {
		return $this->errors;
	}
	
	public function is_valid() {
		return empty($this->errors);
	}
	
	public function validate($validates = array()) {
		$this->validates = $validates;
		return $this;
	}	
	
	public function name($name = null) {
		if (is_null($name)) {
			return $this->name;
		}
		$this->name = $name;
		return $this;
	}
	
	public function level($level = null) {
		if (is_null($level)) {
			return $this->level;
		}
		$this->level = $level;
		return $this;
	}
	
	public function points($points = null) {
		if (is_null($points)) {
			return $this->points;
		}
		$this->points = $points;
		return $this;
	}
	
	public function race($race = null) {
		if (is_null($race)) {
			return $this->rece;
		}
		$this->race = $race;
		return $this;
	}
	
	public function experience($experience = null) {
		if (is_null($experience)) {
			return $this->experience;
		}
		$this->experience = $experience;
		return $this;
	}
	
	public function strength($strength = null) {
		if (is_null($strength)) {
			return $this->strength;
		}
		$this->strength = $strength;
		return $this;
	}
	
	public function agility($agility = null) {
		if (is_null($agility)) {
			return $this->agility;
		}
		$this->agility = $agility;
		return $this;
	}

	public function vitality($vitality = null) {
		if (is_null($vitality)) {
			return $this->vitality;
		}
		$this->vitality = $vitality;
		return $this;
	}
	
	public function energy($energy = null) {
		if (is_null($energy)) {
			return $this->energy;
		}
		$this->energy = $energy;
		return $this;
	}
	
	public function money($money = null) {
		if (is_null($money)) {
			return $this->money;
		}
		$this->money = $money;
		return $this;
	}
	
	public function map($map = null) {
		if (is_null($map)) {
			return $this->map;
		}
		$this->map = $map;
		return $this;
	}	

	public function position_x($position_x = null) {
		if (is_null($position_x)) {
			return $this->position_x;
		}
		$this->position_x = $position_x;
		return $this;
	}	

	public function position_y($position_y = null) {
		if (is_null($position_y)) {
			return $this->position_y;
		}
		$this->position_y = $position_y;
		return $this;
	}

	public function code($code = null) {
		if (is_null($code)) {
			return $this->code;
		}
		$this->code = $code;
		return $this;
	}
	
	public function data() {
		return $this->data;
	}
	
	private function find($name) {
		$result = Mssql::fetch('SELECT
			s.ConnectStat AS connect_stat,
			s.ConnectTM AS last_connection,
			s.ServerName AS server,
			ac.GameIDC AS game_idc,
			c.Name AS name,
			c.cLevel AS level,
			c.Experience AS experience,
			c.Class AS class,
			c.PkLevel AS pk_level,
			c.PkCount AS pk_count,
			c.AccountID AS account,
			c.LevelUpPoint AS points,
			c.MapNumber AS map,
			c.MapPosY AS position_y,
			c.MapPosX AS position_x,
			c.CtlCode AS code,
			c.Strength AS strength,
			c.Dexterity AS dexterity,
			c.Vitality AS vitality,
			c.Energy AS energy,
			c.Money AS money,
			gm.G_Name AS guild
			FROM Character c
			LEFT JOIN AccountCharacter ac ON c.AccountID = ac.ID COLLATE DATABASE_DEFAULT
			LEFT JOIN MEMB_STAT s ON c.AccountID = s.memb___id COLLATE DATABASE_DEFAULT
			LEFT JOIN GuildMember gm ON c.Name = gm.Name COLLATE DATABASE_DEFAULT
			WHERE c.Name = :name[string]
		', array('name' => $name));
		$this->data = $result;
		if (!empty($result)) {
			foreach ($result as $key => $value) {
				if (in_array($key, array_keys(get_class_vars(__CLASS__)))) {
					$this->{$key} = $value;
				}
			}
		}
	}
}

?>